package org.jeecg.modules.engineering.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.engineering.entity.EquipInfo;
import org.jeecg.modules.engineering.entity.EquipSort;
import org.jeecg.modules.engineering.mapper.EquipSortMapper;
import org.jeecg.modules.engineering.service.IEquipSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 设备分类表
 * @Author: jeecg-boot
 * @Date:   2019-08-13
 * @Version: V1.0
 */
@Service
@Slf4j
public class EquipSortServiceImpl extends ServiceImpl<EquipSortMapper, EquipSort> implements IEquipSortService {

    @Autowired
    private EquipSortMapper equipSortMapper;

    @Override
    public List<EquipSort> getEquipSortTree() {
        List<EquipSort> list = equipSortMapper.queryList();
        if(list == null || list.size() <= 0){
            return new ArrayList<>();
        }
        list = bulidTree(list);
        return list;
    }

    @Override
    public boolean checkIsLeaf(List<String> ids) {
        long count = equipSortMapper.selectCountByParentId(ids,ids.size());
        return count == 0;
    }

    @Override
    public void editSort(EquipSort equipSort, String parentIds) {
        if(equipSort.getTopSystemArr() != null && equipSort.getTopSystemArr().size() > 0){
            equipSort.setTopSystem(StringUtils.join(equipSort.getTopSystemArr(),","));
        }else{
            equipSort.setTopSystem(null);
        }
        boolean ok = updateById(equipSort);
        if(ok){
            equipSortMapper.updateParentIds(parentIds,equipSort.getParentIds(),parentIds+equipSort.getId()+",");
        }
    }

    @Override
    public String getParentIds(String id) {
        return equipSortMapper.getParentIds(id);
    }

    @Override
    public List<String> findChildList(String sortId) {
        return equipSortMapper.findChildList(sortId);
    }

    private List<EquipSort> bulidTree(List<EquipSort> treeNodes) {

        List<EquipSort> trees = new ArrayList<>();

        for (EquipSort treeNode : treeNodes) {

            treeNode.setValue(treeNode.getId());
            treeNode.setTitle(treeNode.getSortName());
            treeNode.setKey(treeNode.getId());
            if(StringUtils.isNotBlank(treeNode.getTopSystem())){
                treeNode.setTopSystemArr(Arrays.asList(treeNode.getTopSystem().split(",")));
            }else{
                treeNode.setTopSystemArr(new ArrayList<>());
            }
            if ("".equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }

            for (EquipSort it : treeNodes) {
                if (it.getParentId().equals(treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<>());
                    }
                    treeNode.getChildren().add(it);
                }
            }
        }
        return trees;
    }

}
